/*
 *    Alliciga's Process Partner Project
 * 
 *  Copyright (C) 2006-2011 Alliciga <alliciga@users.sourceforge.net>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 */
 
#include "../include/MonitorThread.h"

MonitorThread::MonitorThread(int _pid, int _level)
{
	task.SetProcess(_pid, "AUTO");
	nT = _level;
}

MonitorThread::~MonitorThread()
{
	monitorPolicyList.clear();
}

MonitorProcess MonitorThread::GetMonitorProcess()
{
	return task;
}

int MonitorThread::GetProcessID()
{
	return task.GetProcessID();
}

string MonitorThread::GetProcessName()
{
	return task.GetProcessName();
}

void MonitorThread::MainLoop()
{
	while(!closing)
	{
		sleep(nT);		// checking process status per nT seconds.
		MonitorPolicyList::iterator r = monitorPolicyList.begin();
		while(monitorPolicyList.end() != r)
		{
			if(r->Match())
				r->Notify(task);
			r++;
		}
	}
}

bool MonitorThread::SetCheckTime(int _nT)
{
	if(_nT > 0 && _nT < 86400)
	{
		nT = _nT;
		return true;
	}
	return false;
}

int MonitorThread::GetCheckTime()
{
	return nT;
}

bool MonitorThread::AddMonitorPolicy(string mpName)
{
	MonitorPolicy *pMP = NULL;

	if(mpName == "BaseMonitorPolicy")
	{
		pMP = new MonitorPolicy();
	} else if(mpName == "MemoryMonitorPolicy")
	{
		string type;
		int value;
		cout << "Enter Memory Policy Type (RESExceed) :";
		cin >> type;
		cout << "Enter the value : ";
		cin >> value;
		pMP = new MemoryMonitorPolicy(type,value);
	}

	monitorPolicyList.push_front(*pMP);
	delete pMP;
	return true;
}

bool MonitorThread::DeleteMonitorPolicy(string _mpName)
{
	bool ok = false;
	MonitorPolicyList::iterator r = monitorPolicyList.begin();
	while(monitorPolicyList.end() != r)
	{
		if(_mpName == r->GetMonitorPolicyName())
		{
			monitorPolicyList.erase(r);
			ok = true;
		}
		r++;
	}
	return ok;
}

list<string> MonitorThread::GetEnabledMonitorPolicy()
{
	list<string> tmp;
	MonitorPolicyList::iterator r = monitorPolicyList.begin();
	while(monitorPolicyList.end() != r)
	{
		tmp.push_front(r->GetMonitorPolicyName());
		r++;
	}
	return tmp;
}

bool MonitorThread::AddNotificationPolicy(string mpName, string npName)
{
	bool ok = false;
	MonitorPolicyList::iterator r = monitorPolicyList.begin();
	while(monitorPolicyList.end() != r)
	{
		if(mpName == r->GetMonitorPolicyName())
		{
			NotificationPolicy *pnp = new NotificationPolicy();
			if(pnp->LinkExtension(npName))
				ok = r->AddNotificationPolicy(*pnp);
			else
				delete pnp;
		}
		r++;
	}
	return ok;
}

bool MonitorThread::DeleteNotificationPolicy(string _mpName, string _npName)
{
	bool ok = false;
	MonitorPolicyList::iterator r = monitorPolicyList.begin();
	while(monitorPolicyList.end() != r)
	{
		if(_mpName == r->GetMonitorPolicyName())
		{
			ok = r->DeleteNotificationPolicy(_npName);
		}
		r++;
	}
	return ok;
}

list<string> MonitorThread::GetEnabledNotificationPolicy(string _mpName)
{
	list<string> tmp;
	MonitorPolicyList::iterator r = monitorPolicyList.begin();
	while(monitorPolicyList.end() != r)
	{
		if(_mpName == r->GetMonitorPolicyName())
			return r->GetNotificationPolicyList();
		r++;
	}
	return tmp;
}

// ---------- End of File ------------
